var BusCon = require("../../core/bus/_con");
var db = require("../../core/db/index");
var config = require("../../core/_include/config");
var ResponObj = require("../_include/responObj");
var BuffCon = require("../../core/buff/_con");
var myfs = require("../_include/fs");
var theme = require("./theme");
var role = require("./role");

function rest_builder(_get_bus, prefix) {
	_get_bus || (_get_bus = role.getBus);
	return {
		prefix: prefix || "/bus/classify",
		get: {
			// "/list": function() {
			// 	var bus = role.getBus(this);
			// 	this.body = ResponObj("json", bus.getClassifys())
			// },
			// //获取无分类的商品列表
			// "/goods_list_without_classify":function () {//TODO

			// },
			// "/list/by_card_factory_id/:card_factory_id": function() { //TODO，归到/bus/card_factory/classify下
			// 	var bus = role.getBus(this);
			// 	var card_factory_id = this.params.card_factory_id;
			// 	// this.body = ResponObj("json", bus._getClassify(classify_id));
			// },
			"/by_id/:classify_id": function() {
				var bus = _get_bus(this);
				var classify_id = this.params.classify_id;
				this.body = ResponObj("json", bus.getClassifyDetail(classify_id));
			},
			"/all_child_goods_by_id/:classify_id": function() {
				var bus = _get_bus(this);
				var classify_id = this.params.classify_id;
				this.body = ResponObj("json", bus.getClassifyAllChildGoods(classify_id));
			},
			"/tree/(:classify_id)?": function() {
				var bus = _get_bus(this);
				var classify_id = this.params.classify_id;
				this.body = ResponObj("json", bus.getClassifyTree(classify_id));
			}
		},
		post: {
			"/add_classify": function() {
				var bus = _get_bus(this);
				var body = this.request.body;
				this.body = ResponObj("json", bus.addClassify(body.classify_name, body.parent_classify_id));
			},
		},
		put: {
			"/update_classify/:classify_id": function() {
				var bus = _get_bus(this);
				var classify_id = this.params.classify_id;
				var body = this.request.body;
				this.body = ResponObj("json", bus.updateClassify(classify_id, body));
			},
			"/move_goods_to_classify/:classify_id": function() {
				var bus = _get_bus(this);
				var classify_id = this.params.classify_id;
				var body = this.request.body;
				var goods_id_list = body.goods_id_list;
				this.body = ResponObj("json", bus.moveGoodsListToClassify(classify_id, goods_id_list));
			},
			"/enable_classify_buff/:classify_id": function() {
				var bus = _get_bus(this);
				var classify_id = this.params.classify_id;
				var body = this.request.body;
				this.body = ResponObj("json", bus.enableClassifyBuff(classify_id,
					body.info,
					body.is_child_enable,
					body.is_cover_child_buff
				));
			},
			"/disable_classify_buff/:classify_id": function() {
				var bus = _get_bus(this);
				var classify_id = this.params.classify_id;
				var body = this.request.body;
				this.body = ResponObj("json", bus.disableClassifyBuff(classify_id,
					body.is_child_keep_enable
				));
			},
			"/classify_update_index/(:parent_classify_id)?": function() {
				var bus = _get_bus(this);
				var parent_classify_id = this.params.parent_classify_id;
				var body = this.request.body;
				var classify_id = body.classify_id;
				var classify_index = body.classify_index;
				this.body = ResponObj("json", bus.updateClassifyIndex(parent_classify_id, classify_id, classify_index));
			},
			"/classify_goods_update_index/:classify_id": function() {
				var bus = _get_bus(this);
				var classify_id = this.params.classify_id;
				var body = this.request.body;
				if (!(body.goods_id && body.goods_index)) {
					throwCusError("参数错误");
				}
				this.body = ResponObj("json", bus.updateClassifyGoodsIndex(classify_id, body.goods_id, body.goods_index, body));
			}
		},
		delete: {
			"/remove_classify_list": function() {
				var bus = _get_bus(this);
				var body = this.request.body;
				var classify_id_list = body.classify_id_list;
				var return_classify_id = body.return_classify_id;
				bus.removeClassifyList(classify_id_list);
				this.body = ResponObj("json", bus.getClassifyDetail(return_classify_id));
			}
		}
	};
};
module.exports = rest_builder;